package cn.techaction.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;


import cn.techaction.dao.ActionAddressDao;
import cn.techaction.pojo.ActionAddress;
@Repository
public class ActionAddressDaoImpl implements ActionAddressDao{
	
    @Resource
    private QueryRunner queryRunner;
	@Override
	public ActionAddress findAddrsById(Integer addrId) {
		String sql="select id,user_id as uid,name,phone,mobile,"
				+"province,city,district,addr,zip,dfault,"
				+"isDel,created,updated from address where id = ? and isDel=0";
		try {
			return queryRunner.query(sql, new BeanHandler<ActionAddress>(ActionAddress.class),addrId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	@Override
	public int insertAddress(ActionAddress address) {
		// TODO Auto-generated method stub
		String sql="insert into address(user_id,name,phone,mobile,province,city,district,addr,zip,dfault,isDel,created,updated)"
				+"values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
		Object[] params= {address.getUid(),address.getName(),address.getPhone(),address.getMobile(),address.getProvince(),address.getCity()
				,address.getDistrict(),address.getAddr(),address.getZip(),address.getDfault(),address.getIsDel(),address.getCreated(),address.getUpdated()};
		try {
			return queryRunner.update(sql,params);
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}
	@Override
	public int findDefaultAddrByUserId(Integer uid) {
		// TODO Auto-generated method stub
		String sql="select count(id) as num from address where user_id=? and dfault =1";
		try {
			return queryRunner.query(sql, new ColumnListHandler<Long>("num"),uid).get(0).intValue();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}

	}
	@Override
	public int updateAddress(ActionAddress address) {
		// TODO Auto-generated method stub
		String sql="update address set updated = ?";
		List<Object> params=new ArrayList<Object>();
		params.add(address.getUpdated());
		if(!StringUtils.isEmpty(address.getName())) {
			sql+=" ,name = ? ";
			params.add(address.getName());
		}
		if(!StringUtils.isEmpty(address.getPhone())) {
			sql+=" ,phone = ? ";
			params.add(address.getPhone());
		}
		if(!StringUtils.isEmpty(address.getMobile())) {
			sql+=" ,mobile = ? ";
			params.add(address.getMobile());
		}
		if(!StringUtils.isEmpty(address.getProvince())) {
			sql+=" ,province = ? ";
			params.add(address.getProvince());
		}
		if(!StringUtils.isEmpty(address.getCity())) {
			sql+=" ,city = ? ";
			params.add(address.getCity());
		}
		if(!StringUtils.isEmpty(address.getDistrict())) {
			sql+=" ,district = ? ";
			params.add(address.getDistrict());
		}
		if(!StringUtils.isEmpty(address.getAddr())) {
			sql+=" ,addr = ? ";
			params.add(address.getAddr());
		}
		if(!StringUtils.isEmpty(address.getZip())) {
			sql+=" ,zip = ? ";
			params.add(address.getZip());
		}
		if(address.getDfault()!=null) {
			sql+=" ,dfault = ?";
			params.add(address.getDfault());
		}
		if(address.getIsDel()!=null) {
			sql+=" ,isDel = ?";
			params.add(address.getIsDel());
		}
		sql+=" where id = ?";
		params.add(address.getId());
		try {
			return queryRunner.update(sql,params.toArray());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
	@Override
	public List<ActionAddress> findAddrsByUserId(Integer userId) {
		// TODO Auto-generated method stub
		String sql="select id,user_id as uid,name,phone,"
				+ "mobile,province,city,district,addr,zip,dfault,"
				+ "isDel,created,updated from address where user_id=? and isDel = 0 "
				+ "order by dfault desc,updated desc";
		try {
			return queryRunner.query(sql, new BeanListHandler<ActionAddress>(ActionAddress.class),userId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}
	@Override
	public ActionAddress findDefaultAddr(Integer userId) {
		// TODO Auto-generated method stub
		String sql="select id,user_id as uid,name,phone,mobile,province,city,district,addr,zip,dfault,"
				+ "isDel,created,updated from address where user_id = ? and dfault =1 and isDel =0";
		try {
			return queryRunner.query(sql, new BeanHandler<ActionAddress>(ActionAddress.class),userId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		
	}
	@Override
	public ActionAddress findAddrById(Integer addrid) {
		// TODO Auto-generated method stub
		String sql = "select id,user_id as uid,name,phone,mobile,province,city,district,addr,zip,dfault,"
				+ "isDel,created,updated from address where id = ?";
		try {
			return queryRunner.query(sql,new BeanHandler<ActionAddress>(ActionAddress.class),addrid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}

}
